<template>
	<view class="margin-top bg-white padding-lr">
		<u-form :model="info" ref="form" label-width="auto">
			<u-form-item label="应用标识" prop="clientId" required><u-input v-model="info.clientId" /></u-form-item>
			<u-form-item label="应用密钥" prop="clientSecret" required><u-input v-model="info.clientSecret" /></u-form-item>
			<u-form-item label="应用名称" prop="clientName" required><u-input v-model="info.clientName" /></u-form-item>
			<u-form-item label="访问范围" prop="scopes" required><u-input v-model="info.scopes" /></u-form-item>
			<u-form-item label="权限类型" prop="authorizedGrantTypes" required>
				<cmp-select v-model="info.authorizedGrantTypes" :list="authorizedGrantTypeOptions" label-name="label" value-name="value"></cmp-select>
			</u-form-item>
			<u-form-item label="令牌有效期" prop="accessTokenValiditySeconds" required><u-input v-model="info.accessTokenValiditySeconds" type="number" /></u-form-item>
			<u-form-item label="令牌刷新" prop="refreshTokenValiditySeconds" required><u-input v-model="info.refreshTokenValiditySeconds" type="number" /></u-form-item>
			<view class="padding-tb"><u-button type="primary" @click="save">保存</u-button></view>
		</u-form>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.info) {
			this.info = JSON.parse(decodeURIComponent(option.info));
		}
	},
	onReady: function() {
		this.$refs.form.setRules(this.rules);
	},
	data() {
		return {
			show: false, // 显示选项
			info: {
				id: '',
				clientId: '',
				clientSecret: '',
				scopes: '',
				authorizedGrantTypes: '',
				accessTokenValiditySeconds: '',
				refreshTokenValiditySeconds: ''
			},
			rules: {
				clientId: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				clientSecret: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				clientName: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				scopes: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				authorizedGrantTypes: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				accessTokenValiditySeconds: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				refreshTokenValiditySeconds: [{ required: true, message: '必填', trigger: ['change', 'blur'] }]
			},
			authorizedGrantTypeOptions: [
				{
					value: 'client_credentials',
					label: '客户端凭证'
				},
				{
					value: 'password',
					label: '密码'
				}
			]
		};
	},
	methods: {
		// 保存用户
		save: function() {
			this.$refs['form'].validate(valid => {
				if (valid) {
					this.$request.post(
						'/admin/sys/client/save',
						this.info,
						res => {
							this.$utils.showModal('保存成功！', () => {
								uni.navigateBack({
									delta: 1
								});
							});
						},
						false,
						true,
						true
					);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
